<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2020/8/11
 * Time: 下午2:06
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FinanceUnion extends Base
{
    protected $financeUnionSettleUserKey = "FINANCE:UNION:SETTING:SETTLE:%d";
    protected $financeUnionWithdrawUserKey = "FINANCE:UNION:SETTING:WITHDRAW:%d";
    protected $financeUnionSettingTimeLimit = 30 * 24 * 3600;

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    /**
     * 检测当前公会设置结算用户/提现用户状态
     * @param $unionId
     * @param int $type [0：检测结算用户   1：检测提现用户]
     * @return mixed
     */
    public function getFinanceUnionSettingStatus($unionId, $type = 0)
    {
        if ($type) {
            $key = sprintf($this->financeUnionWithdrawUserKey, $unionId);
            $result = Redis::get($key);
            return $result;
        }
        $key = sprintf($this->financeUnionSettleUserKey, $unionId);
        $result = Redis::get($key);
        return $result;
    }


    /**
     * 设置结算用户Id
     * @param $unionId
     * @param $userId
     * @param $settleUserId
     */
    public function setSettleUserId($unionId, $userId, $settleUserId)
    {
        $key = sprintf($this->financeUnionSettleUserKey, $unionId);
        $nowTime = date("Y-m-d H:i:s", time());
        DB::INSERT("INSERT INTO zb_finance_union_setting (`union_id`,`user_id`,`settle_user_id`,`created_at`) VALUE({$unionId}, {$userId},{$settleUserId},'{$nowTime}') ON DUPLICATE KEY UPDATE settle_user_id={$settleUserId},updated_at = '{$nowTime}'");
        Redis::setex($key, $this->financeUnionSettingTimeLimit, time() + $this->financeUnionSettingTimeLimit);
    }


    /**
     * 获取结算用户Id
     * @param $unionId
     * @return int
     */
    public function getSettleUserId($unionId)
    {
        $result = DB::table('finance_union_setting')->where(['union_id' => $unionId])->first(['settle_user_id']);
        if (empty($result)) {
            return 0;
        }
        $result = self::objToArray($result);
        return $result['settle_user_id'];
    }

    /**
     * 设置提现用户Id
     * @param $unionId
     * @param $userId
     * @param $withdrawUserId
     */
    public function setWithdrawUserId($unionId, $userId, $withdrawUserId)
    {
        $key = sprintf($this->financeUnionWithdrawUserKey, $unionId);
        $nowTime = date("Y-m-d H:i:s", time());
        DB::INSERT("INSERT INTO zb_finance_union_setting (`union_id`,`user_id`,`withdraw_user_id`,`created_at`) VALUE({$unionId}, {$userId},{$withdrawUserId},'{$nowTime}') ON DUPLICATE KEY UPDATE withdraw_user_id={$withdrawUserId},updated_at = '{$nowTime}'");
        Redis::setex($key, $this->financeUnionSettingTimeLimit, time() + $this->financeUnionSettingTimeLimit);
    }


    /**
     * 获取提现用户Id
     * @param $unionId
     * @return int
     */
    public function getWithdrawUserId($unionId)
    {
        $result = DB::table('finance_union_setting')->where(['union_id' => $unionId])->first(['withdraw_user_id']);
        if (empty($result)) {
            return 0;
        }
        $result = self::objToArray($result);
        return $result['withdraw_user_id'];
    }

    /**
     * 添加公会设置日志记录
     * @param $unionId
     * @param $settleUserId
     * @param $withdrawUserId
     * @param $setType
     */
    public function createUnionSetLog($unionId, $settleUserId = 0, $withdrawUserId = 0, $setType = 0)
    {
        $nowTime = date("Y-m-d H:i:s", time());
        $data = [
            'union_id' => $unionId,
            'settle_user_id' => $settleUserId,
            'withdraw_user_id' => $withdrawUserId,
            'set_type' => $setType,
            'created_at' => $nowTime,
            'updated_at' => $nowTime,
        ];
        DB::table('finance_union_setting_log')->insert($data);
    }


    /**
     * 解绑指定结算用户/提现用户
     * @param int $unionId 公会Id
     * @param int $type 操作类型 [0: 解绑结算用户Id   1：解绑提现用户Id]
     */
    public function unbindUser($unionId, $type)
    {
        $nowTime = date("Y-m-d H:i:s", time());
        if ($type) {
            DB::table('finance_union_setting')->where(['union_id' => $unionId])->update(['withdraw_user_id' => 0, 'updated_at' => $nowTime]);
        } else {
            DB::table('finance_union_setting')->where(['union_id' => $unionId])->update(['settle_user_id' => 0, 'updated_at' => $nowTime]);
        }
    }


    /**
     * 获取公会指定结算用户/提现用户ID
     * @param int $unionId 公会Id
     * @param int $type 操作类型 [0：获取结算用户信息   1：获取提现用户信息]
     * @return array
     */
    public function getAppointUserInfo($unionId, $type)
    {
        if ($type) {
            $result = DB::table('finance_union_setting')
                ->where('union_id', '=', $unionId)
                ->first(['withdraw_user_id','user_id']);
        } else {
            $result = DB::table('finance_union_setting')
                ->where('union_id', '=', $unionId)
                ->first(['settle_user_id','user_id']);
        }
        if (empty($result)) {
            return [];
        }
        $result = self::objToArray($result);
        return $result;
    }


    /**
     * 获取绑定记录信息
     * @param int $unionId 公会Id
     * @return array
     */
    public function getFinanceSettingLog($unionId)
    {
        $baseResult = DB::table('finance_union_setting_log')->where(['union_id' => $unionId])->orderBy('created_at', 'desc')->get();
        if (empty($baseResult)) {
            return [];
        }
        $baseResult = self::objToArray($baseResult);
        return $baseResult;
    }
}